/** 
 * Utilities.java
 * @author Lex
 * 2007-9-2 ����11:53:19
 */
package org.lex.net.uitl;

import java.net.ServerSocket;
import java.net.Socket;

public class NetUtilities {
	public static final char CR = '\r'; // 13
	public static final char LF = '\n'; // 10
	public static final String CRLF = "\r\n";

	public static void closeQuietly(Socket socket) {
		if (null == socket)
			return;
		try {
			socket.close();
		} catch (Exception e) {
		}
	}

	public static void closeQuietly(ServerSocket socket) {
		if (null == socket)
			return;
		try {
			socket.close();
		} catch (Exception e) {
		}
	}

	public static int parseReplyCode(String str) {
		if (null == str || str.length() < 3)
			return -1;
		try {
			return Integer.parseInt(str.substring(0, 3));
		} catch (Exception e) {
			return -1;
		}
	}

	public static boolean hasReplyCode(String str) {
		return -1 != parseReplyCode(str);
	}

	public static boolean isDecimalDight(char ch) {
		switch (ch) {
		case '0':
		case '1':
		case '2':
		case '3':
		case '4':
		case '5':
		case '6':
		case '7':
		case '8':
		case '9':
			return true;
		default:
			return false;
		}
	}

	public static boolean isEndWithCRLF(CharSequence cs) {
		int len = cs.length();
		if (len < 2)
			return false;
		return cs.charAt(len - 1) == LF && cs.charAt(len - 2) == CR;
	}

	public static boolean notCRLF(int ch) {
		return ch != CR && ch != LF;
	}

	public static boolean isCRLF(int ch) {
		return ch == CR || ch == LF;
	}
}
